import os 
import shelve
import time
from tqdm import tqdm

def func():
    """
        这个函数用于计算各个家族样本之间图结构大小是否相似，降低手动查找的时间,
        不太清楚是干啥的了
    """
    a=time.time()
    root_path=r"/home/chenyongwei/function_sim_project/all_data/sampleDatas/functionSim"
    lable_path=r"/home/chenyongwei/function_sim_project/all_data/fam_lables.txt"
    family_to_sample_path=r"/home/chenyongwei/function_sim_project/all_data/family_to_sample.txt"

    samples={}
    fam_lables={}
    for i in os.listdir(root_path):
        tmp=i.split(".")
        if tmp[1]=="dir":
            samples[tmp[0]]=1

    with open(lable_path,"r") as file:
        for line in tqdm(file):
            tmp=line.split("\t")
            if tmp[0] in samples:
                with shelve.open(root_path+"//"+tmp[0]) as file:
                    cg=file["cg"]
                if tmp[1] not in fam_lables:
                    fam_lables[tmp[1]]=[(tmp[0],len(cg))]
                else:
                    fam_lables[tmp[1]].append((tmp[0],len(cg)))
    b=time.time()
    with  open(family_to_sample_path,"a") as file:
        file.write("生成耗时{}\n".format(b-a))
        file.write("所有样本的数量：{}\n".format(len(samples)))
        file.write("家族的数量：{}\n".format(len(fam_lables)))

        for i in fam_lables.keys():
            file.write(i+"\t数量: "+str(len(fam_lables[i]))+"\n")
            for name,lth in fam_lables[i]:
                file.write("\t"+name+"--"+str(lth))
            file.write("\n")

if __name__=="__main__":
    func()